package com.yylending.las.personal.bandcard.cardinfo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.yyfax.common.tools.DensityUtil;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yylending.las.Global;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.base.BaseFragment;
import com.yylending.las.common.widgets.CommonDialog;
import com.yylending.las.common.widgets.CustomGridLayoutManager;
import com.yylending.las.common.widgets.Divider;
import com.yylending.las.common.widgets.TotalDisplayRecycleView;
import com.yylending.las.personal.bandcard.BoundBankCards;

import java.util.ArrayList;

/**
 * Created by HenryZhang on 2016/11/10.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class CardInfoFragment extends BaseFragment implements View.OnClickListener, CommonDialog.ButtonClickListener {
    private final static String TAG = "CardInfoFragment";
    protected final static String ARG_BANK_INFO = "bank_info";
    @BindView(R.id.bank_card_rv)
    TotalDisplayRecycleView mBankCardRv;
    @BindView(R.id.service_num_tv)
    TextView mServiceNumTv;

    //已绑定银行卡信息
    private ArrayList<BoundBankCards.BankInfo> mBankInfo = new ArrayList<>();
    Unbinder unbinder;
    /**
     * 客服电话
     */
    private String mPhoneNumber;
    /**
     * 拨打客服电话Dialog
     */
    private CommonDialog mCallDialog;

    public static BaseFragment newInstance(ArrayList<BoundBankCards.BankInfo> bankInfo) {
        BaseFragment fragment = new CardInfoFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(ARG_BANK_INFO, bankInfo);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_card_info, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initData() {
        ArrayList<BoundBankCards.BankInfo> bankInfoArrayList = getArguments().getParcelableArrayList(ARG_BANK_INFO);
        if (bankInfoArrayList != null) {
            mBankInfo.addAll(bankInfoArrayList);
        }

        CustomGridLayoutManager LayoutManager = new CustomGridLayoutManager(getActivity(), 1);
        LayoutManager.setScrollEnabled(false);
        mBankCardRv.setLayoutManager(LayoutManager);

        mBankCardRv.addItemDecoration(new Divider(DensityUtil.dip2px(getActivity(), 10f), getResources().getColor(R.color.color_f1f3f7), false,
                0, 0, 0, DensityUtil.dip2px(getActivity(), 10f)));
        BankCardAdapter adapter = new BankCardAdapter(mBankInfo);
        mBankCardRv.setAdapter(adapter);

        String text = mServiceNumTv.getText().toString();
        int textLength = text.length();
        int startIndex = textLength - 13;
        int endIndex = textLength - 1;
        mPhoneNumber = text.substring(startIndex, endIndex);
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        ForegroundColorSpan redSpan = new ForegroundColorSpan(getContext().getResources().getColor(R.color.color_4b7cf9));
        builder.setSpan(redSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        mServiceNumTv.setHighlightColor(getResources().getColor(android.R.color.transparent));
        builder.setSpan(new Clickable(this), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mServiceNumTv.setText(builder);
        mServiceNumTv.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    public Loader<Presenter> onCreateLoader(int id, Bundle args) {
        return null;
    }

    @Override
    public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(ARG_BANK_INFO, mBankInfo);
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        readBundle(savedInstanceState);
    }

    private void readBundle(Bundle bundle) {
        if (bundle != null) {
            mBankInfo = bundle.getParcelableArrayList(ARG_BANK_INFO);
        }
    }

    @Override
    public void onClick(View v) {
        showCallDialog();
    }

	@Override
	public void onStart() {
		super.onStart();
		Global.setThirdPartyPageOpenedFlag(false);
	}

	@Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        dismissCallDialog();
        mCallDialog = null;
    }

    @Override
    public void onCancel(CommonDialog dialog) {
        dismissCallDialog();
    }

    @Override
    public void onConfirm(CommonDialog dialog) {
        dismissCallDialog();
        //忽略手势密码
        Global.setThirdPartyPageOpenedFlag(true);
        YJKD.tel(getActivity(), mPhoneNumber.replaceAll("-", ""));
    }

    private void showCallDialog() {
        dismissCallDialog();
        mCallDialog = new CommonDialog(getActivity(), null, mPhoneNumber, getString(R.string.call), getString(R.string.cancel), this, true);
        mCallDialog.show();
    }

    private void dismissCallDialog() {
        if (mCallDialog != null && mCallDialog.isShowing()) {
            mCallDialog.dismiss();
        }
    }

    private class Clickable extends ClickableSpan {
        private final View.OnClickListener mListener;

        public Clickable(View.OnClickListener l) {
            mListener = l;
        }

        /**
         * 重写父类点击事件
         */
        @Override
        public void onClick(View v) {
            mListener.onClick(v);
        }

        /**
         * 重写父类updateDrawState方法  我们可以给TextView设置字体颜色,背景颜色等等...
         */
        @Override
        public void updateDrawState(TextPaint ds) {

        }
    }
}
